﻿using log4net;
using SuperFx;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SuperMoney.AdPlatformCallback.Controllers
{
	public class DianJoyController : Controller
	{
		const string adPlatform = "dianjoy";

		static readonly string secret_key_android = ConfigurationManager.AppSettings[adPlatform + "-android-secret"];
		static readonly string secret_key_ios = ConfigurationManager.AppSettings[adPlatform + "-ios-secret"];

		//
		// GET: /DianJoy/Android

		public ActionResult Android(string snuid, string device_id, string app_id, string currency, string app_ratio, string time_stamp, string token, string ad_name, string pack_name)
		{
			string resultText;
			string orignalString = time_stamp + secret_key_android;
			string local_token = Md5Hash.Compute(orignalString);
			// 签名验证
			if (token == local_token)
			{// 通过
				try
				{
					// 当应用名称或应用包名同时为空时，定为重复调用，忽略
					if (String.IsNullOrEmpty(ad_name) && String.IsNullOrWhiteSpace(pack_name))
					{
						resultText = "200";
					}
					else
					{
						int integral;
						decimal ratio;
						decimal price = 0;
						Int32.TryParse(currency, out integral);
						Decimal.TryParse(app_ratio, out ratio);
						if (ratio != 0)
						{
							price = integral / ratio / 100;
						}

						if (String.IsNullOrWhiteSpace(ad_name))
						{
							ad_name = pack_name;
						}

						AdRecordService.Instance.Create(adPlatform, token, null, 0, device_id, null, ad_name, null, integral, price, app_id, "android", snuid);
						resultText = "200";
					}
				}
				catch (Exception<AdRecordExistsExceptionArgs> ex)
				{
					LogManager.GetLogger("android").Error(token + "点乐广告记录已存在", ex);
					resultText = "200";
				}
				catch (Exception ex)
				{
					LogManager.GetLogger("android").Error(token + "服务器端错误", ex);
					resultText = "500";
				}
			}
			else
			{// 失败
				LogManager.GetLogger("android").Error(Request.Url + "签名验证不通过");
				resultText = "403";
			}

			return Content(resultText);
		}

		//
		// GET: /DianJoy/iOS

		public ActionResult iOS(string snuid, string device_id, string app_id, string currency, string app_ratio, string time_stamp, string token, string ad_name, string pack_name)
		{
			string resultText;
			string orignalString = time_stamp + secret_key_ios;
			string local_token = Md5Hash.Compute(orignalString);
			// 签名验证
			if (token == local_token)
			{// 通过
				try
				{
					// 当应用名称或应用包名同时为空时，定为重复调用，忽略
					if (String.IsNullOrEmpty(ad_name) && String.IsNullOrWhiteSpace(pack_name))
					{
						resultText = "200";
					}
					else
					{
						int integral;
						decimal ratio;
						decimal price = 0;
						Int32.TryParse(currency, out integral);
						Decimal.TryParse(app_ratio, out ratio);
						if (ratio != 0)
						{
							price = integral / ratio / 100;
						}

						if (String.IsNullOrWhiteSpace(ad_name))
						{
							ad_name = pack_name;
						}

						AdRecordService.Instance.Create(adPlatform, token, null, 0, device_id, null, ad_name, null, integral, price, app_id, "ios", snuid);
						resultText = "200";
					}
				}
				catch (Exception<AdRecordExistsExceptionArgs> ex)
				{
					LogManager.GetLogger("iOS").Error(token + "点乐广告记录已存在", ex);
					resultText = "200";
				}
				catch (Exception ex)
				{
					LogManager.GetLogger("iOS").Error(token + "服务器端错误", ex);
					resultText = "500";
				}
			}
			else
			{// 失败
				LogManager.GetLogger("iOS").Error(Request.Url + "签名验证不通过");
				resultText = "403";
			}

			return Content(resultText);
		}

	}
}
